package com.example.hasee.zhcd.Activities.XiaoXi;

import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.hasee.zhcd.Adapters.ImageListAdapter;
import com.example.hasee.zhcd.Base.BaseActivity;
import com.example.hasee.zhcd.Bean.EventMsg;
import com.example.hasee.zhcd.Pictrue.ImagePagerActivity;
import com.example.hasee.zhcd.R;
import com.example.hasee.zhcd.Util.GlobalParameter;
import com.example.hasee.zhcd.Util.ProgressDialogUtils;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by hasee on 2018/6/5.
 */

public class MessageDetailActivity extends BaseActivity {
    private String fid;
    private TextView title;
    private ImageView back;
    private ImageView home;
    private TextView time;
    private TextView newstitle;
    private TextView content;
    private HashMap<String, String> loginmap = (HashMap<String, String>) GlobalParameter.get("loginmap");
    private GridView pics;
    private ArrayList<String> urls = new ArrayList<String>();
    private ImageListAdapter adapter;
    @Override
    public int contentViewId() {
        return R.layout.activity_message_detail;
    }

    @Override
    public void set() {
        title.setText("消息详情");
        back.setOnClickListener(this);
        home.setVisibility(View.INVISIBLE);
        ProgressDialogUtils.createProgressDialog(MessageDetailActivity.this, "正在获取系统消息详情...");
        getMsgDetail();
    }

    @Override
    public void init() {
        fid = getIntent().getExtras().getString("fid");
        title = (TextView) findViewById(R.id.centertitle);
        back = (ImageView) findViewById(R.id.back);
        home = (ImageView) findViewById(R.id.home);
        time = (TextView) findViewById(R.id.time);
        newstitle = (TextView) findViewById(R.id.newstitle);
        content = (TextView) findViewById(R.id.content);
        pics=(GridView)findViewById(R.id.pics_grid);
    }

    @Override
    public void getEventData(EventMsg msg) {
        switch (msg.getAction()) {
            case "Fail":

                Toast.makeText(MessageDetailActivity.this, "操作失败，请检查网络！", Toast.LENGTH_LONG).show();
                break;
            case "ZHMsg_Detail":
                Log.e("ZHMsg_Detail", msg.getMsg());

                try {
                    JSONObject json = new JSONObject(msg.getMsg());
                    String rspcode = json.getString("rspcode");
                    String rspmessage = json.getString("rspmessage");
                    if ("0".equals(rspcode)) {
                        final JSONArray data = json.getJSONArray("data");
                        time.setText(data.getJSONObject(0).getString("fdate"));
                        newstitle.setText(data.getJSONObject(0).getString("ftitle"));
                        content.setText(data.getJSONObject(0).getString("ftext"));
                        JSONArray fpic = data.getJSONObject(0).getJSONArray("fpic");
                        urls.clear();
                        for (int i = 0; i < fpic.length(); i++) {
                            Log.e("url", fpic.getJSONObject(i).getString("pic"));
                            urls.add(fpic.getJSONObject(i).getString("pic"));
                        }
                        adapter = new ImageListAdapter(MessageDetailActivity.this, urls);
                        pics.setAdapter(adapter);
                        pics.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                            @Override
                            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                                Intent intent = new Intent();
                                intent.setClass(MessageDetailActivity.this, ImagePagerActivity.class);
                                intent.putExtra("image_index", i);
                                intent.putExtra("image_urls", urls);
                                startActivity(intent);
                            }
                        });
                        getMsgUpdate();
                    } else {
                        Toast.makeText(MessageDetailActivity.this, rspmessage, Toast.LENGTH_LONG).show();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case "ZHMsg_Renew":
                ProgressDialogUtils.closeProgressDialog();
                break;
        }
    }

    private void getMsgDetail() {
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("strFID", fid);
        httpUtils.get("ZHMsg_Detail", map, "ZHMsg_Detail");
    }

    private void getMsgUpdate() {
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("strPhone", loginmap.get("phone"));
        map.put("strFID", fid);
        httpUtils.get("ZHMsg_Renew", map, "ZHMsg_Renew");
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
        }
    }
}
